import {createRouter, createWebHistory,createWebHashHistory,createMemoryHistory} from 'vue-router'
import HomeView from '../views/Home.vue'
// import menus from './menu2'
// import {useMenusStore} from '@/stores/Menu'
const  routes =[
        // 基础路由
        {
            path:"/",
            redirect:"/login"
        },
         {
            path: '/login',
            name: 'login',
            component: () => import('../views/Login.vue')
        },
        {
            path: '/register',
            name: 'register',
            component: () => import('../views/Register.vue')
        },
        {
            path: '/home',
            name: 'home',
            component: HomeView,   
            //子路由 登录完成之后添加动态路由 
        },
    ]
const router = createRouter({
    history: createWebHistory(),
    // history:createMemoryHistory(),
    routes
})
router.beforeEach((to, from, next) => {
    console.log('路由前置导航守卫==', to, from)
    let token  = localStorage.getItem("token")
    // const  menusStore  = useMenusStore()
    //1 判断如果路由访问的是登录页面之间放行
    if(to.path==='/login'||to.path==='/'){
        return next()
    }
    //判断是否有token 没有重定向到login页面
    if(!token&&to.path!='/login'){
        console.log(22)
        return next({path:'/login',replace:true})
    } 
    // console.log('get》》',router.getRoutes())
    // 正常访问页面
    next() 
    
})
const title = 'owep后台管理'
export function getPageTitle(pageTitle) { // 拼接每个路由页面的名称显示在浏览器
    if (pageTitle) {
        return `${title} - ${pageTitle}`
    }
    return `${title}`
}
// 全局后置守卫
router.afterEach((to, from) => {
    // 动态设置浏览器标题
    document.title = getPageTitle(to.meta.title)
})

export default router
